<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<meta charset="utf-8">
<head th:include="include :: header"></head>
<link th:href="@{/ajax/libs/jquery-ztree/3.5/css/metro/zTreeStyle.css}" rel="stylesheet"/>
<body class="white-bg">
	<div class="wrapper wrapper-content animated fadeInRight ibox-content">
		<form class="form-horizontal m" id="form-role-edit" th:object="${menu}">
			<input id="dailyId" name="dailyId" type="hidden" th:field="*{dailyId}"/>
			<div class="form-group">
				<label class="col-sm-3 control-label ">日期：</label>
				<div class="col-sm-8">
					<input class="form-control" type="text" name="dailyDate" id="dailyDate" th:field="*{dailyDate}" readonly="true"/>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">餐别：</label>
				<div class="col-sm-8">
					<select id="dinnerType" name="dinnerType" class="form-control m-b" disabled="disabled">
						<option value="0" th:field="*{dinnerType}">早餐</option>
						<option value="1" th:field="*{dinnerType}">午餐</option>
						<option value="2" th:field="*{dinnerType}">晚餐</option>
						<option value="3" th:field="*{dinnerType}">其他餐</option>
					</select>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">是否开餐：</label>
				<div class="col-sm-8">
					<select id="isOpen" name="isOpen" class="form-control m-b">
						<option value="0" th:field="*{isOpen}">不开餐</option>
						<option value="1" th:field="*{isOpen}">开餐</option>
					</select>
					<span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 选择开餐时，请选择菜品</span>
				</div>
			</div>
			<div class="form-group" id="dishList" style="display: none">
				<label class="col-sm-3 control-label">菜品：</label>
				<div class="col-sm-8">
					<div id="dishTrees" class="ztree"></div>
				</div>
			</div>
			<div class="form-group">
				<div class="form-control-static col-sm-offset-9">
					<button type="submit" class="btn btn-primary">提交</button>
					<button onclick="$.modal.close()" class="btn btn-danger" type="button">关闭</button>
				</div>
			</div>
		</form>
	</div>
	<div th:include="include::footer"></div>
	<script th:src="@{/ajax/libs/jquery-ztree/3.5/js/jquery.ztree.all-3.5.js}"></script>
	<script type="text/javascript">
	
	    $(function() {
	    	
	    	//alert($("#dailyId").val()+$("#dinnerType").val());
			var url = ctx + "dailymenu/dishlist/dailyMenuDishTreeData?dailyId=" + $("#dailyId").val()+"&dinnerType=" + $("#dinnerType").val();
			
			//var url = ctx + "dailymenu/dishlist/menuDishTreeData?menuId=" + $("#menuId").val()+"&dinnerType=" + $("#dinnerType").val();
			var options = {
				id: "dishTrees",
		        url: url,
		        check: { enable: true, nocheckInherit: true, chkboxType: { "Y": "ps", "N": "ps" } },
		        expandLevel: 2
		    };
			$.tree.init(options);
			$("#isOpen").change();
	    });

		$("#form-role-edit").validate({
			submitHandler:function(form){
				edit();
			}
		});
		
		function edit() {
            var dailyId = $("input[name='dailyId']").val();
            var dishIds = $.tree.getCheckedNodes();
            var isOpen = $("#isOpen").val();
            var dinnerType = $("#dinnerType").val();
            
			$.ajax({
				cache : true,
				type : "POST",
				url : ctx + "dailymenu/everyday/rule",
				data : {
					"dailyId": dailyId,
					"dishIds": dishIds,
					"isOpen": isOpen,
					"dinnerType": dinnerType
				},
				async : false,
				error : function(request) {
					$.modal.alertError("系统错误");
				},
				success : function(data) {
					$.operate.saveSuccess(data);
				}
			});
		}
		
		$("#isOpen").change(function(event){
        	var isOpen = $(event.target).val();
        	isOpenVisible(isOpen);
        });
		
		function isOpenVisible(isOpen) {
			if (isOpen == 1) {
	    		$("#dishList").show();
	    	} else {
	    		$._tree.checkAllNodes(false);
	    		$("#dishList").hide();
	    	}
		}
				
		// h固定弹窗的按钮
		function isScrollY() {
// 			如果有滚动条
		   	if(document.documentElement.scrollHeight > document.documentElement.clientHeight){
		   		$(".btn-primary").css({ 'position' :'fixed', top:$(window).height()-50,right:150 })
				$(".btn-danger").css({ 'position' :'fixed', top:$(window).height()-50,right:80 })
				
				$(window).scroll(function(){
					$(".btn-primary,.btn-danger").css({
						top: $(window).scrollTop()+($(window).height()-50)
					});
				})
		   		
		   	}
		}
		setTimeout(isScrollY,500)
	</script>
</body>
</html>
